<?php
include("common.inc.php");
InitGP(array("action","refuid","refuname","referer","aid","cityid")); //初始化變量全局返回
checkclose();//檢查站點是否關閉
//設置團購地區
if (!empty($cityid)) {
	include_once(INC_PATH."/city.class.php");
	$city=new CityClass();
	$city->setcity($cityid);
	$_cityid=$city->getcity();
	$_cityname=$city->getcityname($_cityid);
}
//團購卷查詢處理代碼
if($action=="bondajax"){
	InitGP(array("action","sn","pwd"),'GP',1); //初始化變量全局返回
	include(INC_PATH."/groupbond.class.php");
	$bond=new GroupBondClass();
	if (!empty($sn) && !empty($pwd)){
		$wherestr="sn='{$sn}' and password='{$pwd}' and state in(1,3)";
		$cachedata=$bond->getdata('1',$wherestr,'','');
		if (!empty($cachedata)) {
			//存在
			$wherestr="sn='{$sn}' and password='{$pwd}'";
			editstate("groupbond","state",$wherestr,2);
			editstate("groupbond","usetime",$wherestr,$timestamp);
			$msg="{$sn}消費成功!";
		}else {
			//不存在
			$wherestr="sn='{$sn}' and state in(1,3)";
			$cachedata=$bond->getdata('1',$wherestr,'','');			
			if (!empty($cachedata)) {			
			$msg="團購卷密碼錯誤!";
			}else{
			$msg="團購編號無效!";
			}
		}
	}elseif (!empty($sn) && empty($pwd)){
		$wherestr="sn='{$sn}' and state=1";
		$cachedata=$bond->getdata('1',$wherestr,'','');
		if (!empty($cachedata)) {
		    editstate("groupbond","state",$wherestr,3);
			//存在
			$msg="{$sn}有效！預約成功！";
		}else {
			//不存在
			$msg="{$sn}無效！";
		}
	}else {
		//缺少必要參數
		$msg="請輸入團購卷編號！";
	}
	echo iconv(CHARSET, 'UTF-8',$msg);
	exit;
}
include(INC_PATH."/group.class.php"); //包含團購類
$gp=new GroupClass();
//判斷後台是否設置首頁顯示團購ID
if(GetNum($cfg_index_group)){
	$gid=GetNum($cfg_index_group);
}else {
	//自動顯示當天ID團購
	$todaystart=strtotime(date('Y-m-d 0:0:0',time()));
	$now=time();
	$todayend=$todaystart+86400;
	//地區限制條件
	if (!empty($_cityid)) {
		$citywhere=" and cityid=".$_cityid;		
	}
	$wheresql="group_end_time >= $now and state = 1 {$citywhere}";
	//$wheresql="TO_DAYS(group_begin_time) = TO_DAYS(NOW())";
	$returnarray = $gp->getdata(1,"",$wheresql);
	if(!empty($returnarray)){
		$gid=$returnarray[0]['gid'];//設置查詢到的gid
	}
	unset($returnarray);	//清空數組
	
}
//處理邀請返利開始********************
if($action=="referrals"){
	//這裡可以增加一個寫日誌操作，記錄每次邀請來訪
	if(!empty($refuid)){
		include(INC_PATH."/member.class.php"); //包含團購類
		$mobj=new memberclass();
		$refuname=$refuname?$refuname:$refuid;
		$mtemp=$mobj->getone($refuname);
		$refuname=$mtemp['uname'];
		unset($mobj);
	}
	if(!empty($refuname)){
		include(INC_PATH."/referrals.class.php"); //包含團購類
		$ref=new Referralsclass();
		$ref->set($refuname);
		unset($ref);
	}
}
//處理邀請返利結束**********************
if (empty($gid)) {
	dheader("mail.php");//php跳轉頁面
}
//$gid=9;						//指定團購ID 後台設置或者指定獲取當天的gid
$value=$gp->getone($gid); 	//獲取團購信息

//未結束的其他團購
//地區限制條件
$cwherestr[]="G.state=1";
if (!empty($_cityid)) {
	$cwherestr[]="G.cityid=".$_cityid;
}
if (!empty($gid)) {
	$cwherestr[]="G.gid<>".$gid;
}
$cwherestr[]="G.group_state <> 3";
$cwherestr[]="G.group_end_time > ".$timestamp;
if(!empty($cwherestr)) $cwheresql = implode(' AND ', $cwherestr);	//條件匯總
$otherarray=$gp->getdata("5","IMG",$cwheresql,"","G.*,A.filepath,A.thumb"); //獲取團購數據

//未結束的其他團購獲取結束

include(INC_PATH."/guestbook.class.php"); //包含團購類
$gb=new GuestBook();
$gbcount=$gb->getcount("state=1");	//獲取留言總數
$gbarray=$gb->getdata(3,"state=1");	//獲取三條留言

//檢測當前登錄用戶是否已經下過訂單
if (!empty($_USERS)) {
	include(INC_PATH."/order.class.php");
	$o=new OrderClass();
	
	$ovalue=$o->getdata("1","gid='{$gid}' and money_state=0 and state<>2 and uname='{$_USERS[uname]}'"); //獲取團購數據
	if (!empty($ovalue[0])) {
		$ordervalue=$ovalue[0];
	}
}

include(INC_PATH."/article.class.php");
$art=new ArticleClass();

$datagg=$art->getdata('','typeid=3');

$subject = "{$value['title']}";
$body = "那武價吼康啦【{$cfg_site_name}】今天端出來的超值好康就是...

登登登~

『{$value['title']}』

手腳慢的人就搶不到啦!!!

{$cfg_site_url}";
//$body = "發現一好網站--{$cfg_site_name}他們每天組織一次團購，超值！今天的團購是：{$value['title']}我想您會感興趣的：{$cfg_site_url}";
$subject= iconv("UTF-8","BIG5//IGNORE", $subject);
$body= iconv("UTF-8","BIG5//IGNORE", $body);
$subject = urlencode($subject);
$body = urlencode($body);

/* 取得尚未過期的團購資訊 */
unset($cwherestr);
unset($gwherestr);
//$todaystart=strtotime(date('Y-m-d 0:0:0',time()));
$todaystart=time();
	$cwherestr[]="state=1";
	$cwherestr[]="group_end_time >={$todaystart}";
	$cwherestr[]="group_begin_time < {$todaystart}";
	$gwherestr[]="G.state=1";
	$gwherestr[]="G.group_end_time >={$todaystart}";
	$gwherestr[]="G.group_begin_time < {$todaystart}";	
	if(!empty($cwherestr)) $cwheresql = implode(' AND ', $cwherestr);	//條件匯總
	if(!empty($gwherestr)) $gwheresql = implode(' AND ', $gwherestr);	//條件匯總
	
	
	//獲取當前頁碼
	$total=$gp->getcount($cwheresql); 							  //總信息數
	//$pagesize=8;												  //一頁顯示信息數
	
	if(!empty($cfg_site_pagenum))$pagesize=$cfg_site_pagenum;
	$page = isset($page) ? max(1, intval($page)) : 1;             //處理頁碼變量
	$offset=($page-1)*$pagesize;   								  //偏移量
	$dataarray=$gp->getdata("$offset,$pagesize","IMG",$gwheresql,"","G.*,A.filepath,A.thumb"); //獲取團購數據
	

$timestamp = time();


//用ajax 取得各團購剩下時間 回傳 秒數
if($_POST["gettimestamp"] == "acton"){
	foreach ($dataarray as $date=>$value){
		$groupon_end_time[] = array("gid"=>$value['gid'] ,"end_timestamp" => $value['group_end_time']-time() );
	}
	$groupon_end_time = json_encode($groupon_end_time);
	echo  $groupon_end_time;
	exit;
}

/***********************/	
include template('index');//包含輸出指定模板
?>